package com.lxl.controller;

import com.lxl.pojo.Teacher;
import com.lxl.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/login")
    public String login(Teacher teacher, HttpServletRequest request) {
        System.out.println("*****************************");
        try {
            Teacher t = teacherService.login(teacher);
            if (t != null) {
                request.getSession().setAttribute("teacher", t);
                request.setAttribute("msg", "欢迎登录");
                return "main";
            }
            request.setAttribute("msg", "用户名或密码错误");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "系统错误");
            return "login";
        }
        return null;
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        try {
            HttpSession session=request.getSession();
            session.invalidate();
            return "login";
        }catch (Exception e){
            e.printStackTrace();
            return "main";
        }
    }


}
